package com.heima.admin.job;

import com.heima.admin.service.WemediaNewsAutoScanService;
import com.heima.feigns.WemediaFeign;
import com.heima.model.common.dtos.ResponseResult;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class PublishNewsJob {

    @Autowired
    private WemediaNewsAutoScanService wemediaNewsAutoScanService;
    @Autowired
    private WemediaFeign wemediaFeign;
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    @XxlJob("publishNews")
    public ReturnT publishNews(String param) {
        log.info("发布任务执行器开始执行=========>");
        //获取所有待发布的文章
        ResponseResult releaseRes = wemediaFeign.findRelease();
        if (releaseRes.getCode() != 0) {
            log.error("wemediaFeign 远程调用 findRelease()出错，错误原因：{}", releaseRes.getErrorMessage());
            return ReturnT.FAIL;
        }
        //获取所有待发布的文章
        List<Integer> newsList = (List<Integer>) releaseRes.getData();
        System.out.println("newsList = " + newsList);
        //远程调用发布文章的方法
        if (newsList == null || newsList.size() == 0) {
            log.info("当前暂无待发布文章");
            return ReturnT.SUCCESS;
        }
        for (Integer id : newsList) {

            wemediaNewsAutoScanService.autoScanByMediaNewsId(id);
        }
        log.info("自媒体文章发布成功11");
        return ReturnT.SUCCESS;
    }
}
